package com.haiertvbic.tvnow.service;

/**
 * Big Endian Data Convertor, 高位在前，低位在后。高位先发，低位后发
 * 
 * @author 孟繁峰
 * @version 1.0
 */
public final class BigEndianConvertor {
	private static final short MASK = 0x00FF;

	private BigEndianConvertor() {
	}

	public static byte[] toBytes(int data) {
		byte[] result = new byte[4];
		result[3] = (byte) (data & MASK);
		result[2] = (byte) ((data >> 8) & MASK);
		result[1] = (byte) ((data >> 16) & MASK);
		result[0] = (byte) ((data >>> 24) & MASK);
		return result;
	}

	public static byte[] toBytes(short data) {
		byte[] result = new byte[2];
		result[1] = (byte) (data & MASK);
		result[0] = (byte) ((data >>> 8) & MASK);
		return result;
	}

	public static int toInteger(byte[] data) {
		int result = 0;
		result |= 0x000000FF & data[3];
		result |= 0x0000FF00 & (data[2] << 8);
		result |= 0x00FF0000 & (data[1] << 16);
		result |= 0xFF000000 & (data[0] << 24);
		return result;
	}

	public static short toShort(byte[] data) {
		short result = 0;
		result |= 0x00FF & data[1];
		result |= 0xFF00 & data[0] << 8;
		return result;
	}
}
